<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

if(isset($_SESSION['u_nic'])) {
	include('php/lib/panel_inc.php');
	if(isset($_POST['pays'])) {
		$adresse = mysql_real_escape_string($_POST['adresse']);
		$cp = preg_replace('#([^0-9])+#i', '', $_POST['cp']);
		$ville = preg_replace('#([^A-Z éèêàâôîù])+#i', '', strtoupper($_POST['ville']));
		$pays = intval($_POST['pays']);
		$tel = preg_replace('#([^0-9\+\.])+#i', '', $_POST['tel']);
		$naissance = mktime(0,0,0,intval($_POST['naissance_mon']),intval($_POST['naissance_day']),intval($_POST['naissance_yea']));
		$error = 1;
		if(($adresse == "") OR ($cp == "") OR ($ville == "") OR ($pays == "") OR ($tel == "")) {
			$error = 2;
		}
		if(date('Y', $naissance) < date('Y', time() -  3600*24*365*25)) {
			$error = 3;
		}
		if(!preg_match('`^\+[0-9]{1,3}\.[0-9]{4,12}$`',$tel)) {
			$error = 4;
		}
		if(preg_match('`^\+33\.6[0-9]{8}$`',$tel)) {
			$error = 5;
		}
		if($error == 1) {
			query("UPDATE users SET u_adresse = '".$adresse."', u_cp = '".$cp."', u_ville = '".$ville."', u_pays = '".$pays."', u_tel = '".$tel."', u_naissance = '".date('Y-m-d',$naissance)." 00:00:00' WHERE u_nic = '".$_SESSION['u_nic']."'");
		}
		$tpl->assign(array(
			'error' => $error,
			'load' => "new Effect.Pulsate(document.getElementById('error'))"
		));
	}
	$infos = mysql_fetch_array(query("SELECT 
										u_civilite,
										u_nom,
										u_prenom,
										u_email,
										u_adresse,
										u_cp,
										u_ville,
										u_pays,
										u_tel,
										u_email,
										UNIX_TIMESTAMP(u_naissance)
										FROM users WHERE u_nic = '".$_SESSION['u_nic']."'"));

	include ROOT.'php/lib/registrars/online.inc.php';
	$_domgest = new bh_doms_gest();
	$_liste_pays = $_domgest->codes_pays();
	$_domgest->deconnect();
	$tpl->assign(array(
		'u_civilite' => $infos['u_civilite'],
		'u_nom' => $infos['u_nom'],
		'u_prenom' => $infos['u_prenom'],
		'u_adresse' => $infos['u_adresse'],
		'u_cp' => $infos['u_cp'],
		'u_ville' => $infos['u_ville'],
		'u_adresse' => $infos['u_adresse'],
		'u_tel' => $infos['u_tel'],
		'u_email' => $infos['u_email'],
		'u_naissance_yea' => date('Y', $infos['UNIX_TIMESTAMP(u_naissance)']),
		'u_naissance_mon' => date('m', $infos['UNIX_TIMESTAMP(u_naissance)']),
		'u_naissance_day' => date('d', $infos['UNIX_TIMESTAMP(u_naissance)']),
		'code_pays' => $_liste_pays['retfields']
	));
	$title = "Modifier vos informations personnelles";
	$tplinc = "panel-general-infospersos";
	$parse = 1;
	$tpl->assign(array(
		'iconesleft' => 1
	));
}
else { header("Location:/site/connexion.html"); }
?>